<?php
function getTables () {
	$kolom2 = (object) array("a","b");
	$table1 = array("nama" => "Satu", "kolom" => (object) array("a","b"));
	$table2 = array("nama" => "Dua", "kolom" => $kolom2);
	$tables = array(
		(object) array(
			"kolom"=>array(
				(object) array(
					"field"=>"1",
					"values"=>null,
				),
				(object) array(
					"field"=>"2",
					"values"=>array(
						"value"=>"value",
						"text"=>"text"
					)
				),
				(object) array(
					"field"=>"3",
					"values"=>null,
				)
			)),
		(object) array(
			"kolom"=>"2"));
	$output = (object) array('tables' => $tables);
	return $output;
};
function getDb () {
	$query = "SHOW TABLES;";
	$rs = mysql_query("$query");
	while ($obj = mysql_fetch_row($rs)){
		$arr[] = $obj[0];
	};
	return $arr;
};

function getCol ($table) {
	$query = "SHOW COLUMNS FROM $table;";
	$rs = mysql_query("$query");
	while ($obj = mysql_fetch_assoc($rs)){
		$arr[] = $obj;
	};
	return $arr;
};

function getFkRef ($table) {
	$query = "select column_name, referenced_table_name
		from
    		information_schema.key_column_usage
		where
			referenced_table_schema = 'sira'
		AND
			table_name = '$table'
		AND
			referenced_table_name is not null;";
	$rs = mysql_query("$query");
	while ($obj = mysql_fetch_assoc($rs)){
		$arr[] = $obj;
	};
	return $arr;
};

function getFk ($table, $kolom) {
	$query = "SELECT id AS value, $kolom AS text FROM $table;";
	$rs = mysql_query("$query");
	while ($obj = mysql_fetch_assoc($rs)){
		$arr[] = $obj;
	};
	return $arr;
};

function create ($table,$param) {
	$cols = getCol($table);
	//columns = isi dari kolom 'Field' dijadikan key, untuk nantinya di intersect
	foreach ($cols as $col){
		$field = $col['Field'];
		$columns[$field] = null;
	};
	//val param = array assoc dari kolom dan value yang berguna untuk insert
	$valParam = array_intersect_key($param,$columns);
	//lab val param = val param key dijadikan value
	while(list($key,$val) = each($valParam)){
		$labValParam[] = $key;
	};
	$col = implode(",",$labValParam);
	$columns = "(".$col.")";
	$values = implode("','",$valParam);
	$query = "INSERT INTO $table $columns VALUES ('".$values."');";
	$rs = mysql_query($query);
	if ($rs) {
		echo json_encode($rs);
	}
	else {
		header("HTTP/1.1 500 Internal Server Error");
		echo "Insert failed";
	}
};

function read ($table) {
	$query = "SELECT * FROM $table;";
	$rs = mysql_query("$query");
	while($obj = mysql_fetch_object($rs)) {
		$arr[] = $obj;
	}
	echo json_encode($arr);
};

function update ($table,$param) {
	$cols = getCol($table);
	//columns = isi dari kolom 'Field' dijadikan key, untuk nantinya di intersect
	foreach ($cols as $col){
		$field = $col['Field'];
		$columns[$field] = null;
	};
	//val param = array assoc dari kolom dan value yang berguna untuk update
	$valParam = array_intersect_key($param,$columns);
	//lab val param = val param key dijadikan value
	while(list($key,$val) = each($valParam)){
		if ($val){
			$setEach[] = "$key='$val'";
		}
	};
	$set = implode(",",$setEach);
	$query = "UPDATE $table SET $set WHERE id=".$param['id'];
	$rs = mysql_query($query);
	if ($rs) {
		echo json_encode($rs);
	}
	else {
		header("HTTP/1.1 500 Internal Server Error");
		echo "Update failed";
	}
};

function destroy ($table,$param) {
	$query = "DELETE FROM ".$table." WHERE id=".$param['id'];
	$rs = mysql_query($query);
	if ($rs) {
		echo json_encode($rs);
	}
	else {
		header("HTTP/1.1 500 Internal Server Error");
		echo "Update failed";
	}
};

function viewDonatur () {
	$query = "SELECT * FROM view_donatur";
	$rs = mysql_query($query);
	while($obj = mysql_fetch_object($rs)) {
		$arr[] = $obj;
	}
	echo json_encode($arr);
};

?>